
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;
using BA.DAL;
using BA;


namespace BlazeApps.Messaging
{
    [Serializable()]
    public class PrivateMessageChild : Csla.BusinessBase<PrivateMessageChild>
    {
        #region Business Properties and Methods

        //declare members
        private Guid _privatemessageid = Guid.NewGuid();
        private Guid _fromuserid = Guid.NewGuid();
        private string _subject = string.Empty;
        private string _body = string.Empty;
        private SmartDate _createdate = new SmartDate(DateTime.Now);
        private Guid _userprivatemessageid = Guid.NewGuid();
        private Guid _userid = Guid.NewGuid();
        private bool _isviewed = false;
        private string _fromusername = string.Empty;
        private string _tousername = string.Empty;

        [System.ComponentModel.DataObjectField(true, true)]
        public Guid privatemessageid
        {
            get
            {
                CanReadProperty("privatemessageid", true);
                return _privatemessageid;
            }
        }

        public Guid fromuserid
        {
            get
            {
                CanReadProperty("fromuserid", true);
                return _fromuserid;
            }
        }

        public string subject
        {
            get
            {
                CanReadProperty("subject", true);
                return _subject;
            }
            set
            {
                CanWriteProperty("subject", true);
                if (value == null)
                    value = string.Empty;
                if (!_subject.Equals(value))
                {
                    _subject = value;
                    PropertyHasChanged("subject");
                }
            }
        }

        public string body
        {
            get
            {
                CanReadProperty("body", true);
                return _body;
            }
            set
            {
                CanWriteProperty("body", true);
                if (value == null)
                    value = string.Empty;
                if (!_body.Equals(value))
                {
                    _body = value;
                    PropertyHasChanged("body");
                }
            }
        }

        public DateTime createdate
        {
            get
            {
                CanReadProperty("createdate", true);
                return _createdate.Date;
            }
        }

        public string createdateString
        {
            get
            {
                CanReadProperty("createdate", true);
                return _createdate.Text;
            }
            set
            {
                CanWriteProperty("createdate", true);
                if (value == null)
                    value = string.Empty;
                if (!_createdate.Equals(value))
                {
                    _createdate.Text = value;
                    PropertyHasChanged("createdate");
                }
            }
        }

        public Guid userprivatemessageid
        {
            get
            {
                CanReadProperty("userprivatemessageid", true);
                return _userprivatemessageid;
            }
        }

        public Guid userid
        {
            get
            {
                CanReadProperty("userid", true);
                return _userid;
            }
        }

        public bool isviewed
        {
            get
            {
                CanReadProperty("isviewed", true);
                return _isviewed;
            }
            set
            {
                CanWriteProperty("isviewed", true);
                if (!_isviewed.Equals(value))
                {
                    _isviewed = value;
                    PropertyHasChanged("isviewed");
                }
            }
        }

        public string fromusername
        {
            get
            {
                CanReadProperty("fromusername", true);
                return _fromusername;
            }
        }

        public string tousername
        {
            get
            {
                CanReadProperty("tousername", true);
                return _tousername;
            }
        }

        protected override object GetIdValue()
        {
            return _privatemessageid;
        }

        #endregion //Business Properties and Methods

        #region Validation Rules
        private void AddCustomRules()
        {
            //add custom/non-generated rules here...
        }

        private void AddCommonRules()
        {
            //
            // subject
            //
            ValidationRules.AddRule(CommonRules.StringRequired, "subject");
            //
            // body
            //
            ValidationRules.AddRule(CommonRules.StringRequired, "body");
            //
            // createdate
            //
            ValidationRules.AddRule(CommonRules.StringRequired, "createdateString");
        }

        protected override void AddBusinessRules()
        {
            AddCommonRules();
            AddCustomRules();
        }
        #endregion //Validation Rules

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
            //TODO: Define authorization rules in PrivateMessageChild
            //AuthorizationRules.AllowRead("privatemessageid", "PrivateMessageChildReadGroup");
            //AuthorizationRules.AllowRead("fromuserid", "PrivateMessageChildReadGroup");
            //AuthorizationRules.AllowRead("subject", "PrivateMessageChildReadGroup");
            //AuthorizationRules.AllowRead("body", "PrivateMessageChildReadGroup");
            //AuthorizationRules.AllowRead("createdate", "PrivateMessageChildReadGroup");
            //AuthorizationRules.AllowRead("userprivatemessageid", "PrivateMessageChildReadGroup");
            //AuthorizationRules.AllowRead("userid", "PrivateMessageChildReadGroup");
            //AuthorizationRules.AllowRead("isviewed", "PrivateMessageChildReadGroup");

            //AuthorizationRules.AllowWrite("subject", "PrivateMessageChildWriteGroup");
            //AuthorizationRules.AllowWrite("body", "PrivateMessageChildWriteGroup");
            //AuthorizationRules.AllowWrite("createdate", "PrivateMessageChildWriteGroup");
            //AuthorizationRules.AllowWrite("isviewed", "PrivateMessageChildWriteGroup");
        }

        #endregion //Authorization Rules

        #region Factory Methods
        internal static PrivateMessageChild NewPrivateMessageChild()
        {
            return new PrivateMessageChild();
        }

        internal static PrivateMessageChild GetPrivateMessageChild(SafeDataReader dr)
        {
            return new PrivateMessageChild(dr);
        }

        private PrivateMessageChild()
        {

            MarkAsChild();
        }

        private PrivateMessageChild(SafeDataReader dr)
        {
            MarkAsChild();
            Fetch(dr);
        }
        #endregion //Factory Methods


        #region Data Access

        #region Criteria

        [Serializable()]
        private class Criteria
        {
            public Guid privatemessageid;

            public Criteria(Guid privatemessageid)
            {
                this.privatemessageid = privatemessageid;
            }
        }

        #endregion //Criteria

        #region Data Access - Fetch
        private void Fetch(SafeDataReader dr)
        {
            FetchObject(dr);
            MarkOld();
            ValidationRules.CheckRules();

            //load child object(s)
            FetchChildren(dr);
        }

        private void FetchObject(SafeDataReader dr)
        {
            _privatemessageid = dr.GetGuid("privatemessageid");
            _fromuserid = dr.GetGuid("fromuserid");
            _subject = dr.GetString("subject");
            _createdate = dr.GetSmartDate("createdate", _createdate.EmptyIsMin);
            _userid = dr.GetGuid("userid");
            _isviewed = dr.GetBoolean("isviewed");
            _fromusername = dr.GetString("fromusername");
            _tousername = dr.GetString("tousername");
        }

        private void FetchChildren(SafeDataReader dr)
        {
        }
        #endregion //Data Access - Fetch

        #region Data Access - Insert
        internal void Insert(SqlConnection cn, PrivateMessageParent parent)
        {
            if (!IsDirty)
                return;

            ExecuteInsert(cn, parent);
            MarkOld();

            //update child object(s)
            UpdateChildren(cn);
        }

        private void ExecuteInsert(SqlConnection cn, PrivateMessageParent parent)
        {
            using (SqlCommand cm = cn.CreateCommand())
            {
                cm.CommandType = CommandType.StoredProcedure;
                cm.CommandText = "AddPrivateMessageChild";

                AddInsertParameters(cm, parent);

                cm.ExecuteNonQuery();

                _privatemessageid = (Guid)cm.Parameters["@Newprivatemessageid"].Value;
                _fromuserid = (Guid)cm.Parameters["@Newfromuserid"].Value;
                _userprivatemessageid = (Guid)cm.Parameters["@Newuserprivatemessageid"].Value;
                _userid = (Guid)cm.Parameters["@Newuserid"].Value;
            }//using
        }

        private void AddInsertParameters(SqlCommand cm, PrivateMessageParent parent)
        {
            //TODO: if parent use identity key, fix fk member with value from parent
            cm.Parameters.AddWithValue("@subject", _subject);
            cm.Parameters.AddWithValue("@body", _body);
            cm.Parameters.AddWithValue("@createdate", _createdate.DBValue);
            cm.Parameters.AddWithValue("@isviewed", _isviewed);
            cm.Parameters.AddWithValue("@Newprivatemessageid", _privatemessageid);
            cm.Parameters["@Newprivatemessageid"].Direction = ParameterDirection.Output;
            cm.Parameters.AddWithValue("@Newfromuserid", _fromuserid);
            cm.Parameters["@Newfromuserid"].Direction = ParameterDirection.Output;
            cm.Parameters.AddWithValue("@Newuserprivatemessageid", _userprivatemessageid);
            cm.Parameters["@Newuserprivatemessageid"].Direction = ParameterDirection.Output;
            cm.Parameters.AddWithValue("@Newuserid", _userid);
            cm.Parameters["@Newuserid"].Direction = ParameterDirection.Output;
        }
        #endregion //Data Access - Insert

        #region Data Access - Update
        internal void Update(SqlConnection cn, PrivateMessageParent parent)
        {
            if (!IsDirty)
                return;

            if (base.IsDirty)
            {
                ExecuteUpdate(cn, parent);
                MarkOld();
            }

            //update child object(s)
            UpdateChildren(cn);
        }

        private void ExecuteUpdate(SqlConnection cn, PrivateMessageParent parent)
        {
            using (SqlCommand cm = cn.CreateCommand())
            {
                cm.CommandType = CommandType.StoredProcedure;
                cm.CommandText = "UpdatePrivateMessageChild";

                AddUpdateParameters(cm, parent);

                cm.ExecuteNonQuery();

            }//using
        }

        private void AddUpdateParameters(SqlCommand cm, PrivateMessageParent parent)
        {
            cm.Parameters.AddWithValue("@privatemessageid", _privatemessageid);
            cm.Parameters.AddWithValue("@fromuserid", _fromuserid);
            cm.Parameters.AddWithValue("@subject", _subject);
            cm.Parameters.AddWithValue("@body", _body);
            cm.Parameters.AddWithValue("@createdate", _createdate.DBValue);
            cm.Parameters.AddWithValue("@userprivatemessageid", _userprivatemessageid);
            cm.Parameters.AddWithValue("@userid", _userid);
            cm.Parameters.AddWithValue("@isviewed", _isviewed);
        }

        private void UpdateChildren(SqlConnection cn)
        {
        }
        #endregion //Data Access - Update

        #region Data Access - Delete
        internal void DeleteSelf(SqlConnection cn)
        {
            if (!IsDirty)
                return;
            if (IsNew)
                return;

            ExecuteDelete(cn);
            MarkNew();
        }

        private void ExecuteDelete(SqlConnection cn)
        {
            using (SqlCommand cm = cn.CreateCommand())
            {
                cm.CommandType = CommandType.StoredProcedure;
                cm.CommandText = "DeletePrivateMessageChild";

                cm.Parameters.AddWithValue("@privatemessageid", this._privatemessageid);

                cm.ExecuteNonQuery();
            }//using
        }
        #endregion //Data Access - Delete

        #endregion //Data Access
    }
}
